Lesson
1: A Simple Function
Overview
In
this lesson, I will go over with you a very simple function example.
I will explain everything you will need to know about it and then at
the end I will give you a 'challenge'. This challenge contains some
content from the next section, so you will be able
to find out
your answer in Lesson 2.
Now that you've looked at an
introduction to the advantages of JASS, you're probably eager to get
started with some code, right? Now, forget GUI (regular 'triggers'),
JASS is different from GUI, so I'll be treating
it that way. Now,
open up a text editor of some sort such as Notepad (windows) or some
other program
depending on your operating system.
Now, I'm
going to show some code. It will be a bit confusing at first, but
I'll explain it throughout this lesson.
function HelloWorld takes nothing returns nothing |
Now, don't put this into the editor yet
as it's not complete. First off, I'll start with the first
line.
JASS, like many programming languages, is made up of a
series of functions. I won't go into detail
on that yet, but for
now just know that you are declaring/creating a function.
Now,
the next line:
|
call BJDebugMsg("Hello World") |
This particular line 'calls' a function. Functions are like actions if you've used GUI before, they execute a command. This particular function puts the text "Hello World" (without the quotes) onto the screen.
|
endfunction |
Now, the final line is pretty simple,
when you declare/create a function it needs to end somewhere, right?
This line just tells you that the function ends here.
Now,
I've gone over with you a very basic example of JASS, if you wish to
test this function in the World Editor, simply:
Create a new map
Go to the trigger editor
Click on the map icon at the top of the list of triggers
Paste the code in the custom script section.
Now, create a trigger (sorry for the
GUI reference), you can make the event whatever you want, I'd suggest
that you make it go off whenever you press an arrow key. Now, for the
action, just find custom script, and in the box
that it gives you,
type:
|
call HelloWorld() |
And there you have it, you can now test
the function in the game.
Notice how you call a function in
that line. I'm now going to go over the function declarations a
bit.
When you call a function, you type
'call <function_name>(
<parameter1>, <parameter2>, <parameterN> )'
What
this basically says is that you type call, while is followed by the
name of the function and parenthesis '()'. If the function needs
certain info (known as parameters) you put that specific info inside
the parenthesis. Each set of 'info' is separated by a comma ','
This
particular function only needed a string (message) to display on the
screen. Here's some more info:
When a function has the text 'takes
nothing', that means you don't need anything in the parenthesis
when calling it.
The return value is a value that the function
returns. Like the CreateUnit function returns a unit. If you're
confused, don't worry as this will be covered in a later
lesson.
Now, feel free to mess around with the HelloWorld
function, add more calls to the BJDebugMsg, show whatever text you
want. Let me say that for now, the text should be inside quotation
marks "like this"
Challenge #1: Modify the
HelloWorld function to print out other info:
Here's an
example:
This may seem like a waste of time, but
the more you use JASS the more familiar it will become.
Good
Luck -wyrmlord